# coding=utf-8
"""
    @project: qabot
    
    @file： user.py
    @date：2023/9/4 10:57
    @desc:
"""
from django.core import cache
from drf_yasg.utils import swagger_auto_schema
from rest_framework.decorators import action
from rest_framework.decorators import permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from rest_framework.views import Request
import json
import os
from django.core.files.storage import FileSystemStorage
from common.response import result
from smartdoc.settings import JWT_AUTH
from users.serializers.user_serializers import  CheckCodeSerializer
from django.utils.translation import gettext_lazy as _
from xpack.serializers.platform_serializer import ConfigSerializer
from xpack.serializers.license_serializers import LicenseSerializer

user_cache = cache.caches['user_cache']
token_cache = cache.caches['token_cache']

class Status(APIView):

    @action(methods=['POST'], detail=False)
    @permission_classes((AllowAny,))
    def post(self, request: Request, application_id: str):
        data = {
            "app_id": application_id,
            "platform": request.data.get('type'),
            "status": request.data.get('status')
        }
        ConfigSerializer.Status(data = data).save()
        return result.success(data=data)
    
    @action(methods=['GET'], detail=False)
    @permission_classes((AllowAny,))
    @swagger_auto_schema(operation_summary=_("Check whether the verification code is correct"),
                         operation_id=_("Check whether the verification code is correct"),
                         request_body=CheckCodeSerializer().get_request_body_api(),
                         responses=CheckCodeSerializer().get_response_body_api(),
                         security=[],
                         tags=[_("User")]) 
    def get(self, request: Request, application_id: str):
        data = {
            "app_id": application_id
        }
        info = ConfigSerializer.Status(data = data).list()
        return result.success(data=info)

class Wechat(APIView):
    @action(methods=['POST'], detail=False)
    @permission_classes((AllowAny,))
    def post(self, request: Request, application_id: str):
        data = {
            "app_id": application_id,
            "platform": "wechat",
            "content": json.dumps(request.data)
        }
        ConfigSerializer.Create(data = data).save()
        return result.success(data=data)
    @action(methods=['GET'], detail=False)
    @permission_classes((AllowAny,))
    def get(self, request: Request, application_id: str):
        where = {
            "app_id": application_id,
            "platform": "wechat",
        }
        info = ConfigSerializer.Query(data = where).info()
        return result.success(data=info)
class Wecom(APIView):
    @action(methods=['POST'], detail=False)
    @permission_classes((AllowAny,))
    def post(self, request: Request, application_id: str):
        data = {
            "app_id": application_id,
            "platform": "wecom",
            "content": json.dumps(request.data)
        }
        ConfigSerializer.Create(data = data).save()
        return result.success(data=data)
    @action(methods=['GET'], detail=False)
    @permission_classes((AllowAny,))
    def get(self, request: Request, application_id: str):
        where = {
            "app_id": application_id,
            "platform": "wecom",
        }
        info = ConfigSerializer.Query(data = where).info()
        return result.success(data=info)
class Dingtalk(APIView):
    @action(methods=['POST'], detail=False)
    @permission_classes((AllowAny,))
    def post(self, request: Request, application_id: str):
        data = {
            "app_id": application_id,
            "platform": "dingtalk",
            "content": json.dumps(request.data)
        }
        ConfigSerializer.Create(data = data).save()
        return result.success(data=data)
    @action(methods=['GET'], detail=False)
    @permission_classes((AllowAny,))
    def get(self, request: Request, application_id: str):
        where = {
            "app_id": application_id,
            "platform": "dingtalk",
        }
        info = ConfigSerializer.Query(data = where).info()
        return result.success(data=info)
class Feishu(APIView):
    @action(methods=['POST'], detail=False)
    @permission_classes((AllowAny,))
    def post(self, request: Request, application_id: str):
        data = {
            "app_id": application_id,
            "platform": "feishu",
            "content": json.dumps(request.data)
        }
        ConfigSerializer.Create(data = data).save()
        return result.success(data=data)
    @action(methods=['GET'], detail=False)
    @permission_classes((AllowAny,))
    def get(self, request: Request, application_id: str):
        where = {
            "app_id": application_id,
            "platform": "feishu",
        }
        info = ConfigSerializer.Query(data = where).info()
        return result.success(data=info)

class License(APIView):
    @action(methods=['GET'], detail=False)
    @permission_classes((AllowAny,))
    def get(self, request: Request):
        info = LicenseSerializer.Query(data = request.data).info()
        return result.success(data=info)
    
    @action(methods=['PUT'], detail=False)
    @permission_classes((AllowAny,))
    def put(self, request: Request):
        file = request.FILES.get('license_file')
        content = file.read()
        licensePath = os.environ.get("WLOU_LICENSE")
        if licensePath is not None:
            try:
                with open(licensePath, 'w') as file:
                    file.write(content.decode('utf-8'))
                return result.success(data=True)
            except IOError:
                print("授权文件错误")
                return result.success(data=False)
    
        